home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 351-375 / disk_351 / pdc / libsrc.lzh / LibSrc / SysIO / read.c < prev    next >
C/C++ Source or Header  |  1990-04-07  |  2KB  |  65 lines

  1. /*
  2.  * Libraries and headers for PDC release 3.3 (C) 1989 Lionel Hummel.
  3.  * PDC Software Distribution (C) 1989 Lionel Hummel and Paul Petersen.
  4.  * PDC I/O Library (C) 1987 by J.A. Lydiatt.
  5.  *
  6.  * This code is freely redistributable upon the conditions that this 
  7.  * notice remains intact and that modified versions of this file not
  8.  * be included as part of the PDC Software Distribution without the
  9.  * express consent of the copyright holders.  No warrantee of any
  10.  * kind is provided with this code.  For further information, contact:
  11.  *
  12.  *  PDC Software Distribution    Internet:                     BIX:
  13.  *  P.O. Box 4006             or hummel@cs.uiuc.edu            lhummel
  14.  *  Urbana, IL  61801-8801       petersen@uicsrd.csrd.uiuc.edu
  15.  */
  16.  
  17. /* read.c - read from file
  18.  *
  19.  * int read(fd, buf, bufsize)
  20.  * int fd;
  21.  * char *buf;
  22.  * int bufsize;
  23.  *
  24.  * Returns number of bytes read
  25.  *         0 if at EOF
  26.  *        -1 if there was a DOS error
  27.  */
  28.  
  29. #include    <errno.h>
  30. #include    <fcntl.h>
  31.  
  32. extern long IoErr(), Read();
  33. extern void Chk_Abort();
  34.  
  35. int read(fd, buf, bufsize)
  36. char *buf;
  37. int fd;
  38. int bufsize;
  39. {
  40.     struct _device *p;
  41.     extern struct _device *_devtab;
  42.     int bytes_read;
  43.  
  44.     Chk_Abort();
  45.     p = &_devtab[fd];
  46.     if ( !( 0 <= fd  && fd < _numdev ) || !p->_fileHandle) {
  47.         errno = EBADF;
  48.         return -1;
  49.     }
  50.  
  51.     if ((p->_mode & 0x03) == O_WRONLY) {
  52.         errno = EINVAL;
  53.         return -1;
  54.     }
  55.  
  56.     if ((bytes_read = Read(p->_fileHandle, buf, (long)bufsize)) == -1) {
  57.         errno = IoErr();
  58.         return -1;
  59.     }
  60.  
  61.     Chk_Abort();
  62.  
  63.     return bytes_read;
  64. }
  65.